/*****************************************************************
 * 开发公司或单位：μs.server l.t.d
 * 版权：本文件版权归属us公司研发中心 © 2010-2011
 * 所属项目：news4Desinger
 * 
 * @author 黄显宏
 * @since Jdk6
 * @date 2011-8-15
 *********************************************************************/

package com.monlyu.filter;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

import com.monlyu.util.WebOptUtil;

import java.util.LinkedHashMap;

public class JsCssFileter implements Filter {

    Map<String, String> CACHE = new LinkedHashMap<String, String>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException{

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                                                                                             throws IOException,
                                                                                             ServletException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        if (request instanceof HttpServletRequest) {
            HttpServletResponse res = (HttpServletResponse) response;
            HttpServletRequest req = (HttpServletRequest) request;
            if (req.getRequestURI().endsWith("gz")) {
                if(!res.containsHeader("Content-Encoding")){
                    res.addHeader("Content-Encoding", "gzip");
                }
            }
            res.addHeader("Cache-Control", "max-age=2678400000");
            res.addHeader("Etag", WebOptUtil.APP_ARGS.getVersion());
            chain.doFilter(req, res);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy(){}

    private static String findPath(String first){
        if (first.startsWith("/"))
            first = first.substring(1);
        return first.substring(first.indexOf("/"), first.lastIndexOf("/") + 1);
    }

    public static void main(String[] args){
        System.out.println(findPath("/news/common/scripts/jquery-1.4.3.min.js"));
    }
}
